
<body>

   <div class="hd">
        <div class="warp">
            <ul class="btn-list">
                <li class="item">

                    <a href="<?php echo $base_url?>home/order/shoppingcar" class="shop-car bg">
                        <!--<div class="num">1</div>-->
                        <i class="icon iconfont icon-icon"></i>
                    </a>
                </li>
                <li  class="item">
                     <!--<a href="./register.html">
                        <i class="iconfont icon-wode"></i>请登录
                    </a>-->
                    <?php if(isset($user)){?>
                    <span href="#" class="bg">
                        <i class="icon iconfont icon-wode"></i><?php echo $user->mobile;?>
                    </span>
                    
                    <ul class="child-item">
                            <li><a href="<?php echo $base_url?>home/order/userorders">我的订单</a></li>
                            <li><a href="<?php echo $base_url?>home/address">收货地址</a></li>
                            <li><a href="<?php echo $base_url;?><?php if(!empty($cardid)){echo 'home/card/cardlist';}else{echo 'home/card/addcard';}?>">绑定银行卡</a></li>
                            <li><a href="<?php echo $base_url?>home/account/logout">退出商城</a></li>
                    </ul>
                    <?php }else{?>
                    <a href="<?php echo $base_url?>home/account/login?back_url=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>">
                        <i class="iconfont icon-wode"></i>请登录
                    </a>
                    <?php }?>
                </li>
            </ul>
            <a href="<?php echo $base_url?>home/goods/index">
            <img src="<?php echo $base_url?>static/dist/images/logo.png" alt="" class="logo-img">
             <span class="span_txt">商城首页</span>
            </a>
        </div>
    </div>

    <div class="card-list">
        <div class="warp">
            <div class="tit">
                绑定银行卡
            </div>
            <div class="sm_tit first">
                *此银行卡作为您的返钱账户
            </div>
            <div class="sm_tit">
                *若找不到支行请选择市分行或分行营业部
            </div>
            <div class="form-card">
                <div class="input_txt">
                    <span class="input_tit">持卡人</span>
                    <input type="text" id="username" name='username' value="" maxlength="15" min="2">
                </div>
                <div class="input_txt">
                    <span class="input_tit">银行卡号</span>
                    <input type="text" id="card" name="card" value=""  maxlength="19" min="16">
                </div>
                <div class="input_txt">
                    <span class="input_tit">所属银行</span>
                    <select name="banktypename" id="banktypename" class="sel_input">
                        <option value="">请选择省市区</option>
                        <?php foreach($banklist as $key => $value){?>
                        <option value="<?php echo $value['bankcode'];?>" data="<?php echo $value['banktypename'];?>"><?php echo $value['banktypename'];?></option>
                        <?php }?>
                    </select>
                </div>
                <div class="input_txt sel_txt">
                    <span class="input_tit">开户省份</span>
                    <select name="Province" id="Province" class="sel_1">
                        <option value="">请选择</option>
                       
                    </select>
                     <span class="input_tit2">开户市</span>
                    <select name="City" id="City" class="sel_2">
                        <option value="">请选择</option>
                    </select>
                     <span class="input_tit2">支行</span>
                    <select name="Subbranch" id="Subbranch" class="sel_3">
                        <option value="">请选择</option>
                    </select>
                </div>
                <div class="pd-20"></div>
                <div class="input_txt">
                    <span class="input_tit">&nbsp;</span>
                    <a href="javascript:;" class="save_btn" id='save-btn'>保存</a>
                </div>
            </div>
        </div>
        <div class="pd-100"></div>
    </div>

    <?php $this->load->view('home/pc/comm'); ?>

    <script src="<?php echo $base_url?>static/dist/js/lib/jquery.min.js"></script>
    <script>
        $(function () {
            var swiperSlide = $('.swiper-container').find('.swiper-slide');
            if (swiperSlide.length > 2) {
                var swiper = new Swiper('.swiper-container', {
                    pagination: '.swiper-pagination',
                    paginationClickable: true
                });
            }


            $('.sel_input').on('change',function(){
                var bankcode = $(this).val();
                
                $.ajax({
                    url:"<?php echo $base_url?>home/card/getProvince",
                    data:{'bankcode':bankcode},
                    type:'GET',
                    dataType:'json',
                    success:function(jsonp){
                     
                        if(jsonp.result==0){
                            var html = '';
                            for(i=0;i<jsonp.datas.length;i++){
                                html += '<option value="'+jsonp.datas[i].docname+'" class="active">'+jsonp.datas[i].docname+'</option>'
                            }
                            $('.active').remove();
                            $('.active1').remove();
                            $('.active2').remove();
                            $('.sel_1').append(html);
                        }
                    }
                })
            });


            $('.sel_1').on('change',function(){
                var bankcode = $(".sel_input").find("option:selected").val(),
                    bankname = $(this).val()
               
                $.ajax({
                    url:"<?php echo $base_url?>home/card/getCity",
                    data:{'bankcode':bankcode,'bankname':bankname},
                    type:'GET',
                    dataType:'json',
                    success:function(json){
                     
                        if(json.result==0){
                            var html = '';
                            for(i=0;i<json.datas.length;i++){
                                html += '<option value="'+json.datas[i].city+'" class="active1">'+json.datas[i].city+'</option>'
                            }
                            $('.active1').remove();
                            $('.active2').remove();
                            $('.sel_2').append(html);
                        }
                    }
                })
            });


            $('.sel_2').on('change',function(){
                var bankcode = $(".sel_input").find("option:selected").val(),
                    bankname = $(".sel_1").find("option:selected").val()
                    cityname = $(this).val()
               
                $.ajax({
                    url:"<?php echo $base_url?>home/card/getSubbranch",
                    data:{'bankcode':bankcode,'bankname':bankname,'cityname':cityname},
                    type:'GET',
                    dataType:'json',
                    success:function(jsons){
                        console.log(jsons);
                        
                        if(jsons.result==0){
                            var html = '';
                            for(i=0;i<jsons.datas.length;i++){
                                html += '<option value="'+jsons.datas[i].id+'" class="active2">'+jsons.datas[i].bankname+'</option>'
                            }
                            $('.active2').remove();
                            $('.sel_3').append(html);
                        }
                    }
                })
            });



            $('#save-btn').on('click',function(){
            var username = $('#username').val();//持卡人姓名
            var card = $('#card').val();//卡号
            var subbranch = $('.sel_3').find("option:selected").val() // $('#subbranch').val();//支行id
            var Province = $('.sel_1').find("option:selected").val() // $('.docname').val();//省份名
            var city = $('.sel_2').find("option:selected").val()// $('.city').val();//城市名
            var bankcode = $(".sel_input").find("option:selected").val();  // $('#submit').val();//银行code
            var bankname = $(".sel_input").find("option:selected").text();  // 银行名
            if(username =='' || !isNaN(username) || username.length < 2 || username.length>15)
            {
                alert('姓名不能为空或者格式不正确');
                return;
            }
            if(card=='' || isNaN(card))
            {
                alert('卡号输入不正确 卡号必须是纯数字哦');
                return;
            }  
            if(card.length < 16)
            {
                alert('卡号长度不够');
                return;
            }
            if(card.length >20)
            {
                alert('卡号太长');
                return;
            }
            if(bankname == '' || city == '' || bankcode == ''|| Province == ''|| subbranch == '')
            {
                alert('银行信息填写不完整');
                return;
            }
            $.ajax({
                url:"<?php echo $base_url?>home/card/addcard",
                data:{'username':username,'card':card,'subbranch':subbranch,'bankname':bankname,'city':city,'bankcode':bankcode},
                type:'POST',
                dataType:'json',
                success:function(resultMsg){
                    if(resultMsg.result==0)
                    {
                        alert('添加银行卡信息成功');
                        window.location.href="<?php echo $base_url?>"+'home/card/cardlist';
                    }
                    if(resultMsg.result!=0)
                    {
                        alert(resultMsg.errmsg);
                    }
                }
            })
        })
        })
    </script>
   <?php $this->load->view('home/pc/footer_js');?>
</body>

</html>
